package com.masterclient.app.modules.pickgoods.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.modules.pickgoods.beans.PickGoodsOrderBean;
import com.masterclient.app.widget.PriceIntegralLayout;
import com.masterclient.app.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hxm
 * @date 2020/9/20.
 */
public class PickGoodsOrderListAdapter extends BaseQuickAdapter<PickGoodsOrderBean.ItemBean, BaseViewHolder> {

    private PickGoodsOrderSelectListener listener;

    public PickGoodsOrderListAdapter(PickGoodsOrderSelectListener listener) {
        super(R.layout.view_item_pick_goods_order);
        this.listener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, PickGoodsOrderBean.ItemBean item) {
        CheckBox checkBox = helper.getView(R.id.checkBox);
        TextView tvPayTime = helper.getView(R.id.tv_pay_time);
        TextView tvStatus = helper.getView(R.id.tv_status);
        LinearLayout viewContent = helper.getView(R.id.view_content);
        TextView tvGoodsNum = helper.getView(R.id.tv_goods_num);
        PriceIntegralLayout llAllPrice = helper.getView(R.id.ll_price);
        llAllPrice.setData("","666");

        checkBox.setChecked(item.isChecked);
        checkBox.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                listener.onCheckItem(true,item.orderId,getBuyProduct(item.products));
            } else {
                listener.onCheckItem(false,item.orderId,"");
            }
        });
        tvPayTime.setText(String.format("支付时间：%s", item.payAt));
        tvStatus.setText(item.statusDesc);
        if (TextUtils.isEmpty(item.shippingAmountText)){
            tvGoodsNum.setText(String.format("共 %s 件商品 合计: ", item.productNum));
        }else {
            tvGoodsNum.setText(String.format("共 %s 件商品 合计%s: ", item.productNum, item.shippingAmountText));
        }
        llAllPrice.setData("",item.subtotal);

        //商品信息
        if (item.products != null && item.products.size() > 0) {
            viewContent.removeAllViews();
            for (int i = 0; i < item.products.size(); i++) {
                PickGoodsOrderBean.ItemBean.ProductsBean productBean = item.products.get(i);
                View view = View.inflate(mContext, R.layout.view_pick_goods_order_item, null);
                ImageView ivGood = view.findViewById(R.id.iv_good);
                TextView tvTitle = view.findViewById(R.id.tv_title);
                TextView tvSku = view.findViewById(R.id.tv_skus);
                TextView tv_num = view.findViewById(R.id.tv_num);
                View deviceLine = view.findViewById(R.id.device_line);
                PriceIntegralLayout llPrice = view.findViewById(R.id.ll_price);

                tvTitle.setText(productBean.name);
                tvSku.setText(productBean.attributeText);
                tv_num.setText(String.format("×%s", productBean.buyQty));
                llPrice.setData("",productBean.price);
                ViewTransformUtil.glideImageView(mContext, productBean.image, ivGood,
                        new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0),
                        R.mipmap.defaults, ScreenUtils.dp2px(107), ScreenUtils.dp2px(107));
                if (i == item.products.size()-1){
                    deviceLine.setVisibility(View.GONE);
                }else {
                    deviceLine.setVisibility(View.VISIBLE);
                }
                viewContent.addView(view);
            }
            viewContent.setVisibility(View.VISIBLE);
        } else {
            viewContent.setVisibility(View.GONE);
        }
    }

    /**
     * 获取勾选商品id & 数量
     */
    private String getBuyProduct(List<PickGoodsOrderBean.ItemBean.ProductsBean> dealerProductsBeans) {
        JSONObject buyProductsInfo = new JSONObject();
        try {
            if (dealerProductsBeans != null && dealerProductsBeans.size() > 0) {
                for (int i = 0; i < dealerProductsBeans.size(); i++) {
                    buyProductsInfo.put(dealerProductsBeans.get(i).productId,
                            Integer.parseInt(dealerProductsBeans.get(i).buyQty));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return buyProductsInfo.toString();
    }

    public interface PickGoodsOrderSelectListener {
        void onCheckItem(boolean isCheck,String orderId,String buyProducts);
    }
}
